<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<chiptrack:checkLogon sourcePage="/reasonCodes.do"/>
<%@ page import="java.util.ArrayList,com.scalar.chiptrack.sales.ReasonCode,
                 com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.commons.Constants"%>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>


<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>

<META content="MSHTML 5.50.4807.2300" name=GENERATOR></HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<script language="JavaScript" src="<%=chipTracContext%>/jsp/includes/scripts/validations.js">
</script>

<script language="JavaScript">
    function addMoreText() {
        var htmltext = '<div><table border="0" width="100%">';
        htmltext = htmltext + '<input type="hidden" name="reason_code_id" value="-1">';
        htmltext = htmltext + '<tr><td width="15%" align=center bgcolor="#FFFFEE" class="NArialL"><input type="text" name="reason_code_no" value="" class="txfield" size="10" maxLength=4></td>';
        htmltext = htmltext + '<td width="20%" align=center bgcolor="#FFFFEE" class="NArialL"><select name="reason_code_type" value="" class="txfield"><option value=""></option>';
        htmltext = htmltext + '<option value="OPERATIONS">OPERATIONS</option><option value="FINANCE">FINANCE</option><option value="SALES">SALES</option><option value="MARKETING">MARKETING</option></td>';
        htmltext = htmltext + '<td width="65%" align="center" bgcolor="#FFFFEE" class="NArialL"><input type="text" name="reason_code_name" value="" class="txfield" size=65 maxLength=50><input type="hidden" name="isNew" value="yes"></td>';
        htmltext = htmltext + '</tr></table></div>';
        var pa=document.getElementById("font1");
        pa.insertAdjacentHTML('BeforeEnd',htmltext);
    }

    function submitPage()
    {
        var codeNo = document.taxForm["reason_code_type"];
        var pa = document.getElementById("errors");

		//if((CheckSpecialChar()) && (isValidCodeType()) && (isValidDescription()) )
        if((checkEmptyFields()) && (CheckSpecialChar())  && (isValidDescription()) )
		{
			document.taxForm.action.value="Save";
			document.taxForm.submit();
		}
	}


	function checkEmptyFields()
    {

        var pa = document.getElementById("message");
        // Modified by Prabhanjan on 23 Jan 2003

        if(document.taxForm["reason_code_no"]!=null)
        {
            if(document.taxForm["reason_code_no"].length==null)
            {
                var codeNo = document.taxForm.reason_code_no;
                var description = document.taxForm.reason_code_name;
                var codeType = document.taxForm.reason_code_type;

                if(codeNo!=null)
                {
                if(!((codeNo.value.trim()=="" && description.value.trim()=="" && codeType.value.trim()=="") || (codeNo.value.trim()!="" && description.value.trim()!="" && codeType.value.trim()!="")))
                {
                    if(codeNo.value.trim()=="")
                    {
                        pa.innerHTML = "<li> Enter Reason Code " ;
                        codeNo.select();
                        return false;
                    }
                    if(codeType.value.trim()=="")
                    {
                        pa.innerHTML = "<li> Select Code Type " ;
                        codeType.focus();
                        return false;
                    }
                    if(description.value.trim()=="")
                    {
                        pa.innerHTML = "<li> Enter Description Value " ;
                        description.select();
                        return false;
                    }
                }
                }
            }
            else
            {
                var codeNo = document.taxForm["reason_code_no"];
                var description = document.taxForm["reason_code_name"];
                var codeType = document.taxForm["reason_code_type"];

                for(var i=0;i<codeNo.length;i++)
                {
//                    codeNo[i].value=codeNo[i].value.trim();
//                    description[i].value=description[i].value.trim();

                    if(!((codeNo[i].value.trim()=="" && description[i].value.trim()=="" && codeType[i].value.trim()=="") || (codeNo[i].value.trim()!="" && description[i].value.trim()!="" && codeType[i].value.trim()!="")))
                    {
                        if(codeNo[i].value.trim()=="")
                        {
                            pa.innerHTML = "<li> Enter Reason Code " ;
                            codeNo[i].select();
                            return false;
                        }
                        if(codeType[i].value.trim()=="")
                        {
                            pa.innerHTML = "<li> Select Code Type " ;
                            codeType[i].focus();
                            return false;
                        }
                        if(description[i].value.trim()=="")
                        {
                            pa.innerHTML = "<li> Enter Description Value " ;
                            description[i].select();
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
    function CheckSpecialChar()
	{
		var errors= ''
		var pa = document.getElementById("message");

        //Checking for duplicate reason codes
        // Modified by Prabhanjan on 23 Jan 2003

        if(document.taxForm["reason_code_no"]!=null)
        {
        if(document.taxForm["reason_code_no"].length==null)
        {
            var codeNo = document.taxForm.reason_code_no;
            var NextCodeNo = document.taxForm.reason_code_no;

            if(codeNo!=null)
            {
                var CodeNumber = codeNo.value;

                // Added by Prabhanjan on 27 Jan 2004
                if(CodeNumber.trim()!="" && CodeNumber<=0)
                {
                    pa.innerHTML = "<li> Code Number Cannot be less than or equal to zero" ;
                    codeNo.select();
                    return false;
                }

                for(var j=0; j < CodeNumber.length; j++)
                {
                    var ch = CodeNumber.charAt(j);
                    if(checkInteger(codeNo.value,"Reason Code","Y") == false)
                    {
                        //Commented by Prabhanjan on 27 Jan 2004
                        //pa.innerHTML = "<li> Please enter a valid CodeNo (Numbers only)." ;
                        pa.innerHTML = "<li>" + errMsg + "</li>";
                        codeNo.select();
                        return false;
                    }
                }
            }
        }
        else
        {
            var codeNo = document.taxForm["reason_code_no"];
            var NextCodeNo = document.taxForm["reason_code_no"];

            for(var i=0;i < (codeNo.length) ; i++)
            {
                if(codeNo[i].value!="")
                {
                    for (var j=i+1; j < (NextCodeNo.length); j++)
                    {
                        if(((codeNo[i].value).toLowerCase()) == ((NextCodeNo[j].value).toLowerCase()))
                        {
                            pa.innerHTML = "<li> Duplicate Reason code number.Please enter another Reason code number " ;
                            NextCodeNo[j].select();
                            return false;
                        }
                    }
                }
            }
            //Checking if the reason codes are less than 4 characters and long and are not containing any thing other than integers
            for (var i=0; i < (codeNo.length) ; i++)
            {
                //Commented by Rohan on August 28th 2003 as the max length attribute for thus text field has been set as 4
                /*if(((NextCodeNo[i].value).length) > 4)
                {
                    pa.innerHTML = "<li> The CodeNo should not be more than 4 characters " ;
                    CodeNo[i].select();
                    return false;
                }*/

                var CodeNumber = codeNo[i].value;

                // Added by Prabhanjan on 27 Jan 2004
                if(CodeNumber.trim()!="" && CodeNumber<=0)
                {
                    pa.innerHTML = "<li> Code Number Cannot be less than or equal to zero" ;
                    codeNo[i].select();
                    return false;
                }

                for(var j=0; j < CodeNumber.length; j++)
                {
                    var ch = CodeNumber.charAt(j);

                    if(checkInteger(codeNo[i].value,"Reason Code","Y") == false)
                    {
                        //Commented by Prabhanjan on 27 Jan 2004
                        //pa.innerHTML = "<li> Please enter a valid CodeNo (Numbers only)." ;
                        pa.innerHTML = "<li>" + errMsg + "</li>";
                        codeNo[i].select();
                        return false;
                    }
                }
            }
        }
        }
		return true;
	}

	function isValidDescription()
	{
		var Description = document.taxForm["reason_code_name"];
        var codeNo = document.taxForm["reason_code_no"];
		var pa = document.getElementById("message");

        //for(var i=0;i < (Description.length) ; i++)
		//{
            //Commented by Rohan on August 28th 2003 as the max length attribute for thus text field has been set as 50
			/*if(((Description[i].value).length) > 50)
			{
				pa.innerHTML = "<li> The Description should not be more than 50 characters " ;
				Description[i].select();
				return false;
			}*/

            /*if( ( codeNo[i].value != "") && (Description[i].value == "") )
			{
				pa.innerHTML = "<li> Enter Description Value. " ;
				Description[i].select();
				return false;
			}*/

			/*var descpt = Description[i].value;
			for (var j=0; j<((Description[i].value).length);j++ )
			{
				var c = descpt.charAt(j);
				if(!((c == "-") || (c == "_")||(c == " ")|| (c == ",") || (c == "/") || (c == "#") || (c == "$") || (c == "{") || (c == "}") || (c == "(" ) || (c == ")" ) || (c == "[") || (c == "]") || (isDigit(c)) || (isLetter(c))))
				{
					pa.innerHTML = "<li> Pleace enter valid characters in the Description " ;
					Description[i].select();
					return false;
				}
			}*/
		//}

		return true;
	}

	function isValidCodeType()
	{
		var pa = document.getElementById("message");
		var codeType = document.taxForm["reason_code_type"];
        var codeNo = document.taxForm["reason_code_no"];

        if(codeNo.length==null)
        {
			if(( codeNo.value.trim() != "") && (codeType.value == ''))
			{
				pa.innerHTML = "<li> CodeType should not be null  " ;
				codeType.focus();
				return false;
			}
        }
        else
        {
            for(var k=0; k < codeType.length ; k++)
            {
                if(( codeNo[k].value.trim() != "") && (codeType[k].value == ''))
                {
                    pa.innerHTML = "<li> CodeType should not be null  " ;
                    // codeType[k].select();
                    codeType[k].focus();
                    return false;
                }
            }
        }
		return true;
	}

// SharatH on July 31, 2003
function showErrorReportPage()
{
    newWindow = window.open ('<%=chipTracContext%>/reportProblem.do','reportProblem','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=no,width=750,height=400,resizable=no');
    newWindow.focus();
}
</script>

<!-- Top Header Start-->

<%@ include file="/jsp/includes/header.jsp" %>

<!-- Top Header End-->

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
	<td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>

<!-- Side Navigation start -->

	<%@ include file="/jsp/sales/sales_order_menu.jsp" %>

<!-- end of the side navigations -->
</TD>

<TD bgcolor="#ffffff">
<IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
<TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
<TD valign="top" width="100%">

<IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
<BR>

<!-- from here your body starts -->


<html:form name="taxForm" method="post" action="/reasonCodes.do" type="com.scalar.chiptrack.ChipTrackActionForm">
<center><span class="NArialW"> Reason Codes Setup</span></center>
<img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10>
 <table border="0" width="90%" align=right>
     <tr><td><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
 <tr class='NArialL'><td colspan=2 align="left"><FONT id="message" color="red"></FONT></TD></TR>
 <tr><td class="NArialL"><font color="FF0000"><chiptrack:errors styleClass="NArialL"/></font></td></tr>
 <tr><td class="NArialL"><font color="FF0000"><chiptrack:info styleClass="NArialL"/></font></td></tr>
<tr><td colspan='6' align="left" class='NArialL'><FONT id="errors" size="-2" color='FF0000'></FONT></TD></TR>
	<tr><td>
      <table border="0" width="100%" bordercolor="#FFFFFF">
    <!--This logic:equal is created by Bhaskar on 10 july 2003-->
    <% String sss = (String)request.getAttribute ( "showMessage" );
        if (sss.equalsIgnoreCase( "TRUE" ) ) {%>
        <tr><td colspan=7 align='left'>
        <font class='NArialS'>
        <font color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b>
        </font></td></tr>
        <% } %>
	<tr>
	  <td width="15%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Reason Code</font></td>
          <td width="20%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Code Type</font></td>
	  <td width="65%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Description</font></td>
	</tr>

        <%
                //Get the reason codes
                ArrayList list = (ArrayList) request.getAttribute("reasonCodes");
                ArrayList codeTypes = (ArrayList) request.getAttribute("codeTypes");
                if(list != null) {
                //Loop thru the tax list and show the values
                for(int i=0; i<list.size(); i++)
                {
                    ReasonCode codes = (ReasonCode)list.get(i);
                    String codeNo = codes.getCodeNo();
                    String codeName = codes.getCodeName();
                    String codeType = codes.getCodeType();
         %>
                    <tr>
                        <input type="hidden" name="reason_code_id" value="<%=codeNo%>">
                        <td width="15%" align="center" bgcolor='#FFFFEE' class="NArialL">
                        <input type="text" name="reason_code_no" value="<%=codeNo%>" class="txfield" size="10" maxLength=4></font>
                        <input type="hidden" name="old_reason_code_no" value="<%=codeNo%>">

                        </td>
                        <td width="20%" align="center" bgcolor='#FFFFEE' class="NArialL">
                                <select name="reason_code_type" value="" class='txfield'>
                                    <option value=""></option>
                                    <%

                                        for(int j=0; j<codeTypes.size(); j++) {
                                            String value = (String)codeTypes.get(j);
                                            if((codeType != null) && (codeType.equals(value))) {
                                                %>
                                                <option value="<%=value%>" SELECTED> <%=value%>
                                                <%
                                            } else {
                                                %>
                                                <option value="<%=value%>"> <%=value%>
                                                <%
                                             }
                                        }
                                    %>
                                </select>
                                <input type="hidden" name="old_reason_code_type" value="<%=codeType%>">
                        </td>
                        <td width="65%" align="center" bgcolor='#FFFFEE' class="NArialL"><input type="text" name="reason_code_name" value="<%=codeName%>" class="txfield" size="65" maxLength=50></font>
                        <input type="hidden" name="old_reason_code_name" value="<%=codeName%>">
                        <input type="hidden" name="isNew" value="no">

                        </td>
                    </tr>
        <%
                }
               }
        %>
	<tr><td colspan=3><FONT id="font1"></FONT></TD></TR>
    </table>
    </td></tr>
    <tr><td><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
    <tr><td align=right><a href="javascript:addMoreText();" onmouseover="window.status='Click to Add Reason Codes';return true" onmouseout="window.status='';return true" title="Click here to Add Reason Codes" ><img src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" border=0></a>&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>

    </td></tr>
    <tr><td><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20></td></tr>
    <tr><td align=center>
        <!--Bhaskar added this link on 9 July 2003-->
        <chiptrack:restrictedLink functionality='<%=Functionality.REASON_CODE_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' styleClass="leftsection" href="javascript:submitPage()" onmouseover="window.status='Click to Save Modified Reason Codes';return true" onmouseout="window.status='';return true"><img src='<%=chipTracContext%>/jsp/includes/images/save.gif' border='0' title="Click here to Save Modified Reason Codes"></chiptrack:restrictedLink>
        <!--a href="javascript:submitPage();"><img src='<%=chipTracContext%>/jsp/includes/images/save.gif' border=0></a-->
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="javascript:document.taxForm.reset();"><img src='<%=chipTracContext%>/jsp/includes/images/reset.gif' border='0'  onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset"></a>
    </td></tr>
</table>

<input type="hidden" name="action" value="">
</html:form>



<!-- till here -->

<BR><BR><BR></TD>
<TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>
<!-- Footer starts here -->

<%@ include file="/jsp/includes/footer.jsp" %>

<!-- footer end here -->

</BODY>
</HTML>
